package org.server.manage;
import java.util.*;
import java.net.*;
import java.io.*;
import org.pojo.MessageType;
import org.pojo.User;
import org.pojo.Message.Message;
import org.server.dao.UserDao;
import org.server.tools.ServerTool;
public class Server2ClientThread  extends Thread{

	private Socket s;
	private User u;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	public Server2ClientThread(Socket s,User u, ObjectOutputStream oos, ObjectInputStream ois)
	{
		this.oos = oos;
		this.ois = ois;
		this.u=u;
		this.s=s;
	}
	
	//让该线程去通知其它用户上线
	public void notifyOther()
	{
		//得到所有在线的人的线程
		List<User> hm=ServerTool.onlineUsers;
		Iterator<User> it=hm.iterator();
		
		while(it.hasNext())
		{
			Message m=new Message();
			m.setMesType(MessageType.message_ret_onLineFriend);
			//取出在线人的id
			User us = it.next();
			try {
				if (u.getUserid() == us.getUserid()) continue;
				Server2ClientThread sc=ServerTool.getClientThread(us.getUserid());
				m.setGetterId(u.getUserid());
				m.setCon(u);
				sc.oos.writeObject(m);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
		}
	}
	//让该线程去通知其它用户下线
	private void offlineNotifyOther(){
		//得到所有在线的人的线程
		List<User> hm=ServerTool.onlineUsers;
		Iterator<User> it=hm.iterator();
		while(it.hasNext())
		{
			Message m=new Message();
			m.setMesType(MessageType.message_ret_offLineFriend);
			//取出在线人的id
			User us = it.next();
			try {
				if(us.getUserid() != u.getUserid()){
					Server2ClientThread sc=ServerTool.getClientThread(us.getUserid());
					//m.setGetterId(u.getUserid());
					m.setCon(u);
					sc.oos.writeObject(m);
				}
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
			
		}
	}
	public void run()
	{
	
		Message m = new Message();
		while(true)
		{
			
			//这里该线程就可以接收客户端的信息.
			try {
				m=(Message)ois.readObject();
			    //	System.out.println(m.getSender()+" 给 "+m.getGetter()+" 说:"+m.getCon());
				
				//对从客户端取得的消息进行类型判断，然后做相应的处理
				if(m.getMesType().equals(MessageType.message_comm_textmsg))
				{
					//一会完成转发.
					//取得接收人的通信线程
					Server2ClientThread sc=ServerTool.getClientThread(m.getGetterId());
					sc.oos.writeObject(m);
				}else if(m.getMesType().equals(MessageType.message_get_onLineFriend))
				{
					System.out.println("用户id:"+u.getUserid()+" 获取所有在线人员:");
					
					//把在服务器的在线人员给该客户端返回.
					ServerTool.getAllOnLineUserid();
					Message m2=new Message();
					ServerTool.removeOnlineUser(u);
					m2.setCon(ServerTool.onlineUsers);				
					m2.setMesType(MessageType.message_ret_onLineFriend);
					oos.writeObject(m2);
					ServerTool.addOnlineUser(u);
					
				}else if(m.getMesType().equals(MessageType.message_update_personinfo)){
					UserDao use = new UserDao();
					User updateuser = (User)m.getCon();
					use.userUpdate(updateuser);
					this.u = use.loadUserById(updateuser.getUserid());
					oos.writeObject(m);
					Iterator<User> it=ServerTool.onlineUsers.iterator();
					while(it.hasNext())
					{
						//取出在线人的id
						User us = it.next();
						if(us.getUserid() == u.getUserid()){
							ServerTool.removeOnlineUser(us);
							break;
						}
						
					}
					ServerTool.addOnlineUser(u);
					notifyOther();				
				}
				else if (m.getMesType().equals(MessageType.message_closesocket)){
					System.out.println("用户id:" +u.getUserid()+" 已退出！");
					oos.writeObject(m);
					oos.close();
					ois.close();
					s.close();
					ServerTool.hm.remove(u.getUserid());
					ServerTool.removeOnlineUser(u);
					offlineNotifyOther();
					break;
					
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
				ServerTool.hm.remove(u.getUserid());
				ServerTool.removeOnlineUser(u);
				offlineNotifyOther();
				break;
			}
			
			
		}
		
		
	}

	public Socket getS() {
		return s;
	}

	public void setS(Socket s) {
		this.s = s;
	}
}